<?php


namespace app\admin\controller\jizhan;

use app\admin\model\HtnsActivity;
use app\admin\model\HtnsPageLink;
use app\admin\model\HtnsPageLinkProduct;
use app\admin\model\HtnsProduct;
use app\admin\model\HtnsStoreCategory;
use app\admin\model\HtnsSystemCity;
use app\admin\traits\Curd;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use jianyan\excel\Excel;
use think\App;
use think\facade\Cache;

/**
 * Class Admin
 * @package app\admin\controller\system
 * @ControllerAnnotation(title="基站-新UI-中秋护肤推荐")
 */
class MidutumnRecommended extends AdminController
{

    use Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new HtnsPageLinkProduct();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index($export = 0)
    {
        $pageLinkId = 31;
        if ($this->request->isAjax() || $export) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            if ($export) {
                $page  = 1;
                $limit = 10000;
            }
            $where[] = ['page_link_id','=',$pageLinkId];
            $count = $this->model
                ->where($where)
                ->count();
            $list  = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select()->toArray();
            foreach ($list as &$v) {
                $product = HtnsProduct::find($v['product_id']);
                $v['product_info'] = $product['id'].'-'.$product['store_name'];
            }
            // 导出
            if ($export) {
                return $list;
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        $this->assign('pageLinkId',$pageLinkId);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        $pageLinkId = 31;
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $post['page_link_id'] = $pageLinkId;
                $save               = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('pageLinkId',$pageLinkId);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('pageLinkId', $row['page_link_id']);
        $this->assign('row', $row);
        return $this->fetch();
    }
}